<template>
<div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap"></div>
    <div class="common-level-rail">
      <el-button size="small" type="primary" icon="Plus" @click="addClick"
        >{{$t('plus.add_coupon')}}</el-button
      >
    </div>
    <div class="product-content">
      <el-form ref="form" :model="form" label-width="100px">
        <div class="table-wrap">
          <el-table
            size="small"
            :data="tableData"
            border
            style="width: 100%"
            v-loading="loading"
          >
            <el-table-column
              prop="couponId"
              :label="$t('plus.coupon_id1')"
              width="70"
            ></el-table-column>
            <el-table-column
              prop="couponTypeText"
              :label="$t('plus.coupon_type')"
              width="100"
            ></el-table-column>
            <el-table-column prop="name" :label="$t('plus.coupon_name1')"></el-table-column>
            <el-table-column
              prop="minPrice"
              :label="$t('plus.min_spend_amount')"
            ></el-table-column>
            <el-table-column prop="bigPrice" :label="$t('plus.max_deduction')">
              <template #default="{  row}">
                <el-col v-if="row.bigPrice == 0.0">{{$t('plus.none')}}</el-col>
                <el-col v-else>{{row.bigPrice}}</el-col>
              </template>
            </el-table-column>
            <el-table-column prop="seckillStock" :label="$t('plus.discount_type1')">
              <template #default="scope">
                <div v-if="scope.row.couponType == 10">
                  <span
                    >{{$t('plus.discount_immediate')}} 
<strong class="orange">{{scope.row.reducePrice}}</strong> {{$t('plus.unit_yuan')}}
</span
                  >
                </div>
                <div v-if="scope.row.couponType == 20">
                  <span
                    >{{$t('plus.discount_percent_prefix')}} 
<strong class="orange">{{scope.row.discount / 10}}

</strong> {{$t('plus.discount_percent_suffix')}}
</span
                  >
                </div>
              </template>
            </el-table-column>

            <el-table-column prop="seckillStock" :label="$t('plus.valid_period')">
              <template #default="scope">
                <div v-if="scope.row.expireType == 10">
                  <span
                    >{{$t('plus.valid_days_prefix')}} 
<strong>{{scope.row.expireDay}}</strong> {{$t('plus.valid_days_suffix')}}</span
                  >
                </div>
                <div v-if="scope.row.expireType == 20">
                  <span>
                    {{scope.row.startTimeText}} ~ {{scope.row.endTimeText}}


                  </span>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="receiveNum" :label="$t('plus.total_issued')">
              <template #default="scope">
                <div v-if="scope.row.totalNum == -1">
                  <span>{{$t('plus.no_limit')}}</span>
                </div>
                <div v-else>
                  <span>{{scope.row.totalNum}}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="receiveNum"
              :label="$t('plus.received_count')"
            ></el-table-column>
            <el-table-column prop="sort" :label="$t('plus.sort')"></el-table-column>
            <el-table-column
              prop="createTime"
              :label="$t('plus.create_time')"
              width="135"
            ></el-table-column>
            <el-table-column fixed="right" :label="$t('plus.action')" width="90">
              <template #default="scope">
                <el-button
                  @click="editClick(scope.row)"
                  type="text"
                  size="small"
                  >{{$t('plus.edit')}}</el-button
                >
                <el-button
                  @click="deleteClick(scope.row)"
                  type="text"
                  size="small"
                  >{{$t('plus.delete')}}</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import CouponApi from "@/api/coupon.js";
export default {
  data() {
    return {
      formInline: {},
      form: {},
      tableData: [],
      /*一页多少条*/
      pageSize: 15,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否加载完成*/
      loading: true
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*获取列表*/
    getData() {
      let self = this;
      let Params = {};
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      CouponApi.couponList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.records;
        self.totalDataNumber = res.data.total;
      }).catch(error => {});
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getData();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*添加优惠券*/
    addClick() {
      this.$router.push("/plus/coupon/coupon/add");
    },
    /*修改优惠券*/
    editClick(e) {
      let self = this;
      this.$router.push({
        path: "/plus/coupon/coupon/edit",
        query: {
          id: e.couponId
        }
      });
    },
    /*删除优惠券*/
    deleteClick(e) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        CouponApi.deleteCoupon({
          id: e.couponId
        }, true).then(data => {
          self.loading = false;
          ElMessage({
            message: data.msg,
            type: "success"
          });
          self.getData();
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {
        self.loading = false;
      });
    }
  }
};
</script>

